<template>
  <div class="app-container">
    <div v-if="$permission('identifyQuery:page')">
      <!-- 条件栏 -->
      <NewPageFilter
        v-model:query="filterInfo.query.queryMap"
        :filter-list="filterInfo.searchList"
        :filter-btn="filterInfo.searchBtn"
        :list-type-info="listTypeInfo"
      >
      </NewPageFilter>
      <NewPageFilter :filter-btn="filterInfo.btnList">
        <el-button v-show="true" @click="print">打印</el-button>
        <el-button v-show="true" @click="exportExcel">导出excel</el-button>
      </NewPageFilter>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="getIdentifyQuery"
        :tab-index="true"
        :check-box="false"
        v-model:query="filterInfo.query.queryMap"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        @pageInfo="pageInfo"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-commonName="scope">
          <p>{{ scope.row.commonName || '-' }}</p>
          <p>{{ scope.row.productCode || '-' }}</p>
          <p>{{ scope.row.alias || '-' }}</p>
        </template>
        <template v-slot:col-licenseNumber="scope">
          <p>{{ scope.row.licenseNumber || '-' }}</p>
          <p>{{ scope.row.brandName || '-' }}</p>
        </template>
        <template v-slot:col-standard="scope">
          <p>{{ scope.row.standard || '-' }}</p>
          <p>{{ scope.row.unitName || '-' }}</p>
        </template>
      </TableContainer>
      <ChosePrinter
        :visible="printerInfo.visible"
        @callBack="printerCallBack"
      />
    </div>
    <PageDialog
      v-if="dialogTipsInfo.visible"
      :title="dialogTipsInfo.title"
      v-model:visible="dialogTipsInfo.visible"
      :width="dialogTipsInfo.width"
      :bt-loading="dialogTipsInfo.btLoading"
      :bt-list="dialogTipsInfo.btList"
    >
      <div class="asset-msg">
        <div class="asset-msg-label">
          <i class="el-icon-warning"></i>{{ dialogTipsInfo.msg }}：
        </div>
        <el-radio-group v-model="dialogTipsInfo.flag">
          <el-radio :label="true">当前页数据</el-radio>
          <el-radio :label="false">全部页数据</el-radio>
        </el-radio-group>
      </div>
    </PageDialog>
  </div>
</template>

<script setup name="identifyQuery">
import {
  getIdentifyQuery,
  exportIdentifyQuery,
  printIdentifyQuery,
  printSingleQrCode,
  getProductStatus,
} from '@/api/15189.js'
import { getDepotAll } from '@/api/core'

import { fatchTemplateByCode } from '@/api/base'
const { proxy } = getCurrentInstance()
const dialogTipsInfo = ref({
  visible: false,
  title: '提示',
  btLoading: false,
  type: '',
  msg: '',
  width: '400px',
  flag: true,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '打印',
      type: 'primary',
      event: checkConfirm,
      show: true,
    },
  ],
})
const listTypeInfo = ref({
  depotList: [],
})
const filterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    sortString: '',
    groupMap: {},
    groups: [],
    queryMap: {
      isPagerByPrint: true, //打印是否分页
      isPagerByExport: false, //导出是否分页
    },
    customizeUserVO: {},
  },
  searchList: [],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
  btnList: [
    {
      type: 'button',
      label: '打印',
      event: print,
      show: proxy.$permission('identifyQuery:print'),
    },
    {
      type: 'button',
      label: '导出excel',
      event: exportExcel,
      show: proxy.$permission('identifyQuery:execl'),
    },
  ],
})
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  selections: [],
  fieldList: [
    { label: '仓库', value: 'depotName' },
    {
      label: '通用名称\n编码\n别名',
      value: 'commonName',
      minWidth: 220,
      type: 'slot',
    },
    {
      label: '证号\n生产厂家',
      value: 'licenseNumber',
      minWidth: 220,
      type: 'slot',
    },
    { label: '规格\n单位', value: 'standard', type: 'slot' },
    { label: '生产批号', value: 'batchNumber' },
    { label: '生产日期', value: 'productionDate' },
    { label: '失效日期', value: 'validityDate' },
    {
      label: '数量',
      value: 'stockQty',
      type: 'column',
      formatter: proxy.$fn.qtyFormatter,
    },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '80',
    btList: [
      {
        label: '查看标识',
        event: _printSingleQrCode,
        show: true,
      },
    ],
  },
})
const modifyInfo = ref({
  visible: false,
  title: '',
  orderNo: '',
  returnStoreId: '',
})
const printerInfo = ref({
  visible: false,
  data: {},
})
const page = ref({
  curPage: 1, // 当前页
  pageSize: 20, // 每页条数
})
const clearType = ref(true)

let obj = {
  depotId: {
    label: '仓库',
    value: 'depotId',
    // type: "column",
    componentType: 'select',
    list: 'depotList',
    event: search,
    hidden: false,
    show: true,
  },
}
filterInfo.value.searchList = Object.values({
  ...proxy.$fn.deepClone(obj),
})

getCustomize()

function getCustomize() {
  tableInfo.value.api = getProductStatus
  getList()
  initDepotList()
}
function getList() {
  tableInfo.value.refresh = Math.random()
}
function initDepotList() {
  const params = {
    depotAuth: 'id',
  }
  getDepotAll(params).then((resp) => {
    if (resp && resp.success) {
      listTypeInfo.value.depotList = resp.data.map((item) => {
        return {
          key: item.name,
          value: item.id,
        }
      })
    }
  })
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
function exportExcelList() {
  let params = {
    ...filterInfo.value.query.queryMap,
    ...page.value,
    page: dialogTipsInfo.value.flag,
  }
  exportIdentifyQuery(params).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}

function printList() {
  fatchTemplateByCode({
    resourceCode: 'identifyQuery:print',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      getPrintData(temp)
    }
  })
}
function getPrintData(temp) {
  let params = {
    ...filterInfo.value.query.queryMap,
    ...page.value,
    page: dialogTipsInfo.value.flag,
  }
  printIdentifyQuery(params).then((resp) => {
    if (resp && resp.success) {
      let data = proxy.$fn.deepClone(resp.data)
      proxy.$lodop.preview(temp, data)
    }
  })
}
function exportExcel() {
  //导出
  dialogTipsInfo.value.visible = true
  dialogTipsInfo.value.type = 'excel'
  dialogTipsInfo.value.msg = '请选择数据导出范围'
  dialogTipsInfo.value.btList[1].label = '导出'
  dialogTipsInfo.value.flag = true
}
function print() {
  //打印
  dialogTipsInfo.value.visible = true
  dialogTipsInfo.value.type = 'print'
  dialogTipsInfo.value.msg = '请选择当数据打印范围'
  dialogTipsInfo.value.btList[1].label = '打印'
  dialogTipsInfo.value.flag = true
}
//打印导出
function pageInfo(obj) {
  page.value = obj
}
function checkConfirm() {
  if (dialogTipsInfo.value.type === 'excel') {
    exportExcelList()
  } else if (dialogTipsInfo.value.type === 'print') {
    printList()
  }
  dialogTipsInfo.value.visible = false
  dialogTipsInfo.value.msg = ''
}
function handleClose() {
  dialogTipsInfo.value.visible = false
}
//打印单个二维码
function _printSingleQrCode(row = undefined) {
  if (row.code === null || row.code === undefined) {
    proxy.$message({
      message: '没有生成标识，不能打印',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  }
  let ids = [row.id]
  if (ids.length > 0) {
    printSingleQrCode(ids).then((resp) => {
      if (resp && resp.success) {
        printerInfo.value.visible = true
        printerInfo.value.data = resp.data
      }
    })
  }
}
function printerCallBack(printer) {
  if (printer !== false) {
    proxy.$print.buildRenderBarCode(printerInfo.value.data, printer)
  }
  printerInfo.value.visible = false
}
</script>

<style scoped></style>
